package com.h.chatroom.common.config;

import com.h.chatroom.common.interceptor.AuthHandshakeInterceptor;
import com.h.chatroom.handler.WebSocketHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;

@Slf4j
@Configuration
@EnableWebSocket
public class WebSocketConfig implements WebSocketConfigurer {
    @Autowired
    private WebSocketHandler webSocketHandler;

    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        registry.addHandler(webSocketHandler,"/ws")
                .addInterceptors(new AuthHandshakeInterceptor()) // 添加自定义的拦截器，进行拦截 登录是所用的 Token 进而分析出 userId 和 username
                .setAllowedOrigins("*"); // 允许跨域处理
    }
}
